﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS
{
    public partial class ADDVACCINE : Form
    {
        /** GLOBAL VARIABLES :: being accessed by other forms :: **/
        public int familyNo;
        public string fname;
        public string mName;
        public string sname;
        public string bday;
        public string mom;
        public string checkuptype;

        public string empnum;
        public string empname;

        int doFlag = 0;

        public ADDVACCINE()
        {
            InitializeComponent();
        }

        private void ADDVACCINE_Load(object sender, EventArgs e)
        {
            this.familyNumberMismo.Text = familyNo.ToString();
            this.familyInformationLabel.Text = fname + " " + mName + " " + sname;
            this.bDateInfo.Text = bday;
            this.motherlabel.Text = mom;
            this.checkupTypeInfo.Text = checkuptype;
            this.checkupDate.Text = DateTime.Now.ToShortDateString();
            this.empNumLabel.Text = empnum;
            this.employeeLabel.Text = empname;

            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";
            OleDbConnection conn = new OleDbConnection(conString);
            DataSet ds = new DataSet();
            //opens the connection to the database
            conn.Open();
                //check in the vaccine table if there is an existing record para makuha ang mother's name
                OleDbDataAdapter tagalagay = new OleDbDataAdapter("SELECT * from Vaccine", conn);
                tagalagay.Fill(ds);
                DataTable dt = ds.Tables[0];
                doFlag = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo)&&(dr["FirstName"].ToString()==fname))
                    {
                        this.motherlabel.Text = dr["Mother"].ToString();
                        this.mothertextBox.Enabled = false; //disable Mother textbox
                        doFlag = 0; //success
                        break;
                    }
                }
                //MessageBox.Show(doFlag.ToString());
                if (doFlag == 0) {
                    //check new born screening
                    tagalagay = new OleDbDataAdapter("SELECT * from NewBornScreening", conn);
                    tagalagay.Fill(ds);
                    dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo) && (dr["FirstName"].ToString() == fname))
                        {
                            this.nbspanel.Enabled = false;
                            break;
                        }
                        doFlag = 1;
                    }
                    //check the vaccines
                    tagalagay = new OleDbDataAdapter("SELECT * from DPT", conn);
                    tagalagay.Fill(ds);
                    dt = ds.Tables[0];
                    int counter = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo) && (dr["FirstName"].ToString() == fname))
                        {
                            counter++; //nagbibilang ng DPT
                        }
                    }
                    if (counter == 1){ this.DPT1.Enabled = false; }
                    else if (counter == 2) { this.DPT1.Enabled = false; this.DPT2.Enabled = false; }
                    else if (counter == 3) { this.DPT1.Enabled = false; this.DPT2.Enabled = false; this.DPT3.Enabled = false; }
                    //HEPA
                    tagalagay = new OleDbDataAdapter("SELECT * from Hepa", conn);
                    tagalagay.Fill(ds);
                    dt = ds.Tables[0];
                    counter = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo) && (dr["FirstName"].ToString() == fname))
                        {
                            counter++; //nagbibilang ng Hepa
                        }
                    }
                    if (counter == 1) { this.Hepa1.Enabled = false; }
                    else if (counter == 2) { this.Hepa1.Enabled = false; this.Hepa2.Enabled = false; }
                    else if (counter == 3) { this.Hepa1.Enabled = false; this.Hepa2.Enabled = false; this.Hepa3.Enabled = false; }

                    //BCG
                    tagalagay = new OleDbDataAdapter("SELECT * from BCG", conn);
                    tagalagay.Fill(ds);
                    dt = ds.Tables[0];
                    doFlag = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo) && (dr["FirstName"].ToString() == fname))
                        {
                            this.bcgCheckbox.Enabled = false;
                            break;
                        }
                    }
                    //Toxoid
                    tagalagay = new OleDbDataAdapter("SELECT * from ToxoidVaccine", conn);
                    tagalagay.Fill(ds);
                    dt = ds.Tables[0];
                    doFlag = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == familyNo) && (dr["FirstName"].ToString() == fname))
                        {
                            this.toxoidCheckBox.Enabled = false;
                            break;
                        }
                    }
                }
            conn.Close();
            
        }

        private void BreastFeed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.BreastFeed.Checked)
            {
                this.breastfedDur.Enabled = true;
                this.nfcheckBox.Enabled = false;
            }
            if (!this.BreastFeed.Checked)
            {
                this.breastfedDur.Enabled = false;
                this.breastfedDur.Text = "";
                if (!this.Bottle.Checked)
                {
                    this.nfcheckBox.Enabled = true;
                }
            }
        }

        private void Bottle_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Bottle.Checked)
            {
                this.bottlefedDur.Enabled = true;
                this.nfcheckBox.Enabled = false;
            }
            if (!this.Bottle.Checked)
            {
                this.bottlefedDur.Enabled = false;
                this.bottlefedDur.Text = "";
                if (!this.BreastFeed.Checked)
                {
                    this.nfcheckBox.Enabled = true;
                }
            }
        }

        private void nfcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.nfcheckBox.Checked)
            {
                this.BreastFeed.Enabled = false;
                this.BreastFeed.Checked = false;
                this.breastfedDur.Enabled = false;
                this.breastfedDur.Text = "";

                this.Bottle.Enabled = false;
                this.Bottle.Checked = false;
                this.bottlefedDur.Enabled = false;
                this.bottlefedDur.Text = "";
            }
            if (!this.nfcheckBox.Checked)
            {
                this.BreastFeed.Enabled = true;
                this.breastfedDur.Enabled = true;

                this.Bottle.Enabled = true;
                this.bottlefedDur.Enabled = true;
            }
        }

        private void addVaccineButton_Click(object sender, EventArgs e)
        {
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";
            OleDbConnection conn = new OleDbConnection(conString);
            DataSet ds = new DataSet();
            OleDbDataAdapter tagalagay;
            //opens the connection to the database
            conn.Open();
            if (this.mothertextBox.Enabled)
            {
                tagalagay = new OleDbDataAdapter("Insert into Vaccine "
                    + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "','"
                    + this.mothertextBox.Text + "')", conn);
                tagalagay.Fill(ds);
            }
            if (this.nbspanel.Enabled) {
                if (!this.noNBS.Checked)
                {
                    tagalagay = new OleDbDataAdapter("Insert into NewBornScreening "
                            + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + this.nbsTextBox.Text + "'", conn);
                    tagalagay.Fill(ds);
                }
            }
            /**======= Feeding Information =======**/
            if (this.BreastFeed.Checked)
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                        + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() 
                        + "','" + "BreastFed" + "','" + this.breastfedDur.Text + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Breast Feeding Data Already Exist.";
                    doFlag = 1;
                }
            }
            if (this.Bottle.Checked)
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                        + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                        + DateTime.Now.ToShortDateString() + "','" + "BottleFed" + "','" + this.bottlefedDur.Text + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Bottle Feeding Data Already Exist.";
                    doFlag = 1;
                }
            }
            if (this.nfcheckBox.Checked)
            {
                try
                {
                   tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                        + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                        + DateTime.Now.ToShortDateString() + "','" + "NotFed, 0" + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Feeding Data Already Exist.";
                    doFlag = 1;
                }
            }
            if (this.bcgCheckbox.Checked) {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into BCG "
                         + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                         + DateTime.Now.ToShortDateString() + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "BCG Data Already Exist.";
                    doFlag = 1;
                }
            }
            if (this.toxoidCheckBox.Checked)
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into ToxoidVaccine "
                         + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                         + DateTime.Now.ToShortDateString() + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Toxoid Data Already Exist.";
                    doFlag = 1;
                }
            }
            int counter = 0;
            if (this.DPT1.Enabled) { counter++; }
            if (this.DPT2.Enabled) { counter++; }
            if (this.DPT3.Enabled) { counter++; }
            if (counter < 3 && (this.DPT1.Checked || this.DPT2.Checked || this.DPT3.Checked))
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into DPT "
                         + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                         + DateTime.Now.ToShortDateString() + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "DPT Data Already Exist.";
                    doFlag = 1;
                }
            }
            counter = 0;
            if (this.Hepa1.Enabled) { counter++; }
            if (this.Hepa2.Enabled) { counter++; }
            if (this.Hepa3.Enabled) { counter++; }
            if (counter < 3 && (this.Hepa1.Checked || this.Hepa2.Checked || this.Hepa3.Checked))
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into Hepa "
                         + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" 
                         + DateTime.Now.ToShortDateString() + "')", conn);

                    tagalagay.Fill(ds);
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Feeding Data Already Exist.";
                    doFlag = 1;
                }
            }
            conn.Close();
            HOME home = new HOME();
            home.Visible = true;
            this.Visible = false;
        }

    }
}
